<?php

include '../templateSys/SQL.php';
include '../templateSys/Settings.php';
include '../templateSys/manager_util.php';
include '../templateSys/Admin.php';

//loginCheck
checkUserFinal();

global $link;
$link = getLink();

if (isset($_POST['additem'])) {
    $sectionOwnerName = $_POST['section'];
    $nadpisData = $_POST['nadpis'];
    $nadpisName = generateName();
    createText($nadpisName, $nadpisData, getUser()->getAdminId());

    $popisData = $_POST['popis'];
    $popisName = generateName();
    createText($popisName, $popisData, getUser()->getAdminId());

    $cenaData = $_POST['cena'];
    $cenaName = generateName();
    createInt($cenaName, $cenaData, getUser()->getAdminId());  

    $barvyName = generateName();
    $barvyCisId = generateInt();
    createDyn($barvyName, getUser()->getAdminId(), $barvyCisId);

    $mainImageName = generateName();
    uploadFile("mainImage", $mainImageName);

    $velikostiName = generateName();
    $velikostiCisId = generateInt();
    createDyn($velikostiName, getUser()->getAdminId(), $velikostiCisId);

    $query = sprintf(SQL::$select_ciselnik_velikosti);
    $result = mysql_query($query, $GLOBALS["link"]);
    $countIdVals = 0;
    while ($row = mysql_fetch_array($result)) {
        if (isset($_POST["2_" . $row["idval"]])) {
            $velikostiIdVal[$countIdVals] = $row["idval"];
            $countIdVals++;
            addCiselnikMap($velikostiCisId, 2, $row["idval"]);
        }
    }
    
    $skladName = generateName();
    $skladCisId = generateInt();
    createDyn($skladName, getUser()->getAdminId(), $skladCisId);    

    $typeId = getTypeIdByName($sectionOwnerName);
    $itemId = generateInt();
    createEmptyItemFull("barvy", $typeId, $barvyName, 5, $itemId);
    createEmptyItemFull("velikosti", $typeId, $velikostiName, 5, $itemId);
    createEmptyItemFull("popis", $typeId, $popisName, 1, $itemId);
    createEmptyItemFull("nadpis", $typeId, $nadpisName, 1, $itemId);
    createEmptyItemFull("cena", $typeId, $cenaName, 2, $itemId);
    createEmptyItemFull("img", $typeId, $mainImageName, 4, $itemId);
    createEmptyItemFull("sklad", $typeId, $skladName, 5, $itemId);
    
    mysql_close($link);
    header("location: " . Settings::$rootPath . Settings::$manager. "#i" . $itemId);
    exit();
}

mysql_close($link);
?>